package com.cnfangmao.masterdata.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.dto.BasicDataDependationDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.BasicDataDependation;
import com.cnfangmao.masterdata.service.BasicDataDependationService;
import com.cnfangmao.masterdata.vo.BasicDataDependationVo;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 主数据间依赖关系表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Api(tags = "1-4、配置-主数据依赖关系管理")
@RestController
@RequestMapping("/basicDataDependation")
public class BasicDataDependationController {
	private static final Logger logger = LoggerFactory.getLogger(BasicDataDependationController.class);

	@Autowired
	private BasicDataDependationService basicDataDependationService;

	/**
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "创建主数据依赖关系", notes = "创建主数据依赖关系")
	@ApiImplicitParam(name = "dto", value = "主数据依赖关系", required = true, paramType = "body", dataType = "BasicDataDependationDto")
	@PostMapping("/create.do")
	public ResBody<String> create(@RequestBody BasicDataDependationDto dto) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (dto == null || dto.getSourceMetadataId() == null || dto.getSourceFieldId() == null || 
					dto.getTargetMetadataId() == null || dto.getTargetFieldId() == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，值为空");
				return resBody;
			}
			if (basicDataDependationService.getBasicDataDependationBySourceId(dto.getSourceMetadataId(), dto.getSourceFieldId()) != null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("主数据依赖关系已经存在");
				return resBody;
			}
			
			BasicDataDependation basicDataDependation = ModelUtils.parseObject(dto, BasicDataDependation.class, "create");
			basicDataDependationService.save(basicDataDependation);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
		} catch (Exception e) {
			logger.error(String.format("run create error in BasicDataDependationController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}

	/**
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "更新主数据依赖关系", notes = "更新主数据依赖关系")
	@ApiImplicitParam(name = "dto", value = "主数据依赖关系", required = true, paramType = "body", dataType = "BasicDataDependationDto")
	@PostMapping("/update.do")
	public ResBody<String> update(@RequestBody BasicDataDependationDto dto) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (dto == null || dto.getDependationId() == null || dto.getSourceMetadataId() == null || dto.getSourceFieldId() == null || 
					dto.getTargetMetadataId() == null || dto.getTargetFieldId() == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，值为空");
				return resBody;
			}
			
			BasicDataDependation basicDataDependation = ModelUtils.parseObject(dto, BasicDataDependation.class, "update");

			basicDataDependationService.updateById(basicDataDependation);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("更新成功");
		} catch (Exception e) {
			logger.error(String.format("run update error in BasicDataDependationController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "获取主数据依赖关系", notes = "获取主数据依赖关系")
	@ApiImplicitParam(name = "id", value = "主数据依赖关系的ID", required = true, paramType = "query", dataType = "string")
	@GetMapping("/getById.do")
	public ResBody<BasicDataDependationVo> getById(String id) {
		ResBody<BasicDataDependationVo> resBody = new ResBody<BasicDataDependationVo>();
		try {
			if (id == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，主数据依赖关系的ID为空");
				return resBody;
			}
			BasicDataDependationVo basicDataDependationVo = basicDataDependationService.getBasicDataDependationById(id);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(basicDataDependationVo);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run getById error in BasicDataDependationController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除主数据依赖关系")
	@ApiImplicitParam(name = "ids", value = "主数据依赖关系的ID", required = true, allowMultiple = true, paramType = "query", dataType = "string")
	@PostMapping(value = "/delete.do")
	public ResBody<String> delete(@RequestParam(value = "ids") List<String> ids) {
		ResBody<String> resBody = new ResBody<String>();
		try {
			if (ids == null || ids.size() == 0) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，主数据依赖关系的ID为空");
				return resBody;
			}
			// basicDataDependationService.removeById(ids);
			basicDataDependationService.removeByIds(ids);
			
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
		} catch (Exception e) {
			logger.error(String.format("run delete error in BasicDataRuleController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	/**
	 * 
	 * @param queryCondition
	 * @return
	 */
	@ApiOperation(value = "获取主数据依赖关系列表", notes = "获取主数据依赖关系列表")
	@ApiImplicitParam(name = "searchDto", value = "主数据依赖关系查询条件", required = true, paramType = "body", dataType = "GeneralSearchDto«BasicDataDependationDto»")
	@PostMapping("/search.do")
	public ResBody<GeneralPageVo<BasicDataDependationVo>> search(@RequestBody GeneralSearchDto<BasicDataDependationDto> searchDto) {
		ResBody<GeneralPageVo<BasicDataDependationVo>> resBody = new ResBody<GeneralPageVo<BasicDataDependationVo>>();
		try {
			if (searchDto == null) {
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("参数错误，值为空");
				return resBody;
			}
			
			GeneralPageVo<BasicDataDependationVo> page = basicDataDependationService.getBasicDataDependationsByPage(searchDto);

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(page);
			resBody.message("查询成功");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(String.format("run search error in BasicDataDependationController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}

	/**
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取所有主数据依赖关系列表", notes = "获取所有主数据依赖关系列表")
	@GetMapping("/listAll.do")
	public ResBody<List<BasicDataDependationVo>> listAll() {
		ResBody<List<BasicDataDependationVo>> resBody = new ResBody<List<BasicDataDependationVo>>();
		try {
			List<BasicDataDependationVo> basicDataDependations = basicDataDependationService.getAllBasicDataDependations();

			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(basicDataDependations);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run listAll error in BasicDataDependationController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
}
